package todo;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import todo.dao.Dao;

@SuppressWarnings("serial")
public class ServletCreateTodo extends HttpServlet 
{
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		Utils.checkUser();
		
		System.out.println("Creating new todo ");
		User user = (User) req.getAttribute("user");
		if (user == null) {
			UserService userService = UserServiceFactory.getUserService();
			user = userService.getCurrentUser();
		}

		String editId = req.getParameter("editId");
		String summary = req.getParameter("summary");
		String longDescription = req.getParameter("description");

		if (!Utils.isNull(summary) && !Utils.isNull(longDescription))
		{
			if (editId == null || editId.equals(""))
			{
				Dao.INSTANCE.add(user.getUserId(), summary, longDescription);			
			} 
			else
			{
				Dao.INSTANCE.edit(editId, summary, longDescription);			
			}	
		}

		resp.sendRedirect("/index.jsp");
	}
}